package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Sonic
 * @version : 1.0
 * @createTime : 2024-12-05  19:54
 * @description : 订单管理
 */
@RestController("userOrderController")
@Slf4j
@RequestMapping("/user/order")
@Api("User OrderController Api")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * @param: [ordersSubmitDTO]
     * @return: com.sky.result.Result<com.sky.vo.OrderSubmitVO>
     * @description: 用户下单
     **/
    @PostMapping("/submit")
    @ApiOperation("User submit order")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("###### User submit order:{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return 返回值为第 8步的支付参数
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * @param: []
     * @return: com.sky.result.Result
     * @description: 历史订单分页查询
     **/
    @GetMapping("/historyOrders")
    @ApiOperation("History Orders paging query")
    public Result<PageResult> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("###### History Orders paging query");
        PageResult pageResult= orderService.pageQuery(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @param: [id]
     * @return: com.sky.result.Result<com.sky.vo.OrderVO>
     * @description: 根据 order id查询 Order表中的单条数据
     **/
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("Get Information By Order Id")
    public Result<OrderVO> getByOrderId(@PathVariable Long id) {
        log.info("###### Get Information By Order Id");
        OrderVO orderVO = orderService.getByOrderId(id);
        return Result.success(orderVO);
    }

    /**
     * @param: [id]
     * @return: com.sky.result.Result<com.sky.vo.OrderVO>
     * @description: 取消订单
     **/
    @PutMapping("/cancel/{id}")
    @ApiOperation("Cancel Order By Id")
    public Result<OrderVO> cancelOrder(@PathVariable Long id) throws Exception {
        log.info("###### Cancel Order By Id");
        orderService.cancelOrder(id);
        return Result.success();
    }

    /**
     * @param: [id]
     * @return: com.sky.result.Result
     * @description: 再来一单
     **/
    @PostMapping("/repetition/{id}")
    @ApiOperation("Repetition Order By Id")
    public Result repetition(@PathVariable Long id){
        log.info("###### Repetition Order By Id");
        orderService.repetition(id);
        return Result.success();
    }


    /**
     * @param: [id]
     * @return: com.sky.result.Result
     * @description: 客户催单
     **/
    @GetMapping("/reminder/{id}")
    @ApiOperation("User reminder order")
    public Result reminder(@PathVariable Long id){
        log.info("###### User reminder order");
        orderService.reminder(id);
        return Result.success();
    }
}
